package com.future.controller;

import javax.validation.constraints.NotNull;

import com.future.common.annotation.ControllerMapping;
import com.future.common.base.BaseController;
import com.future.dao.ActivityDao;
import com.future.model.Activity;
import com.future.requestbean.ActivityAdd;
import com.future.requestbean.ActivityUpdate;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.tx.Tx;

@ControllerMapping("/activity")
public class ActivityController extends BaseController {
	@Inject
	private ActivityDao activityDao;

	@Before(Tx.class)
	public void addActivity() {
		ActivityAdd activityAdd = getJsonBody(ActivityAdd.class);

		Activity activity = beanToModel(activityAdd, Activity.class);
		String firstImg = getFirstImg(activityAdd.getContent());
		activity.setHeadimg(firstImg);
		this.activityDao.save(activity);
		renderSuccess();
	}

	@Before(Tx.class)
	public void deleteActivity(@NotNull(message = "选择要删除的对象") Long activeId) {
		this.activityDao.deleteById(activeId);
		renderSuccess();
	}

	public void queryActivity() {
		renderResult("activitys", this.activityDao.findAll());
	}

	@Before(Tx.class)
	public void activetyUpdate() {
		ActivityUpdate activityUpdate = getJsonBody(ActivityUpdate.class);
		String firstImg = getFirstImg(activityUpdate.getContent());
		Activity activity = beanToModel(activityUpdate, Activity.class);
		activity.setHeadimg(firstImg);
		this.activityDao.update(activity);
		renderSuccess();
	}
}
